package com.cskaoyan.app.wxservice.impl;

import com.cskaoyan.app.bean.po.MarketCategory;
import com.cskaoyan.app.bean.po.MarketCategoryExample;
import com.cskaoyan.app.mapper.MarketCategoryMapper;
import com.cskaoyan.app.wxservice.WxCatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WxCatalogServiceImpl implements WxCatalogService {

    @Autowired
    private MarketCategoryMapper categoryMapper;

    @Override
    public Map<String, Object> index() {
        Map<String, Object> map = new HashMap<>();

        MarketCategoryExample example = new MarketCategoryExample();
        example.or().andLevelEqualTo("L1").andDeletedEqualTo(false);
        List<MarketCategory> categoryList = categoryMapper.selectByExample(example);
        MarketCategory currentCategory = categoryList.get(0);
        map.put("categoryList", categoryList);
        map.put("currentCategory", currentCategory);

        Integer pid = currentCategory.getId();
        example.clear();
        example.or().andPidEqualTo(pid).andDeletedEqualTo(false);
        List<MarketCategory> currentSubCategory = categoryMapper.selectByExample(example);
        map.put("currentSubCategory", currentSubCategory);

        return map;
    }

    @Override
    public Map<String, Object> current(Integer id) {
        Map<String, Object> map = new HashMap<>();

        MarketCategory currentCategory = categoryMapper.selectByPrimaryKey(id);
        map.put("currentCategory", currentCategory);

        MarketCategoryExample example = new MarketCategoryExample();
        example.or().andPidEqualTo(currentCategory.getId()).andDeletedEqualTo(false);
        List<MarketCategory> currentSubCategory = categoryMapper.selectByExample(example);
        map.put("currentSubCategory", currentSubCategory);

        return map;
    }
}
